<?php
class MidAutumnService extends AbstractService
{
    const ActivityMidAutumn          = 21;  //中秋节
    /**
     * @var MidAutumnService
     */
    private static $instance;

    /**
     * 单例函数
     * @return MidAutumnService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new MidAutumnService();
        }
        return self::$instance;
    }


    /**
     * 处理创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     * 1. 登录游戏时候发送排行
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {

    }

    /**
     * 提供当前 service 数据
     *
     * @param int $userId
     * @param boolean $visitMode default false
     */
    public function info($userId, $visitMode = false)
    {

    }

    /**
     * 获取中秋活动数据
     * @param $userId
     * @param $activityId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getMidAutumnInfo($userId, $activityId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'commonData' => array(),
            'makeCake' => array(),
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节数据
        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);
        $rewardSituation = '';
        $pointArr = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankPointNum', true)['value']);
        $point = 0;
        if (!$midAutumnData){
            foreach ($pointArr as $key=>$pointStep){
                if ($rewardSituation == ''){
                    $rewardSituation = '0';
                }
                else{
                    $rewardSituation = $rewardSituation.';0';
                }
            }

            $midAutumnData = new MidAutumnVo(
                $userId,//用户ID
                $activityId,//活动ID
                $point,//积分数量
                json_encode($rewardSituation),//积分领奖记录
                $reqTime,//商店可免费获得时间戳
                0,//排行榜领奖标记
                4,//已解锁栏位数量
                $reqTime,//数据创建时间
                $reqTime,//数据更新时间
                $checkData['end'],//结束时间
                true
            );
        }

        if ($midAutumnData->isInsert() && !SzUtility::checkArrayKey($activityId, $midAutumnList->getList())) {
            $midAutumnList->addElement($midAutumnData);
            $midAutumnList->setElement($activityId,$midAutumnData);
        }
        SzPersister::get()->setVoList($midAutumnList);

        //获取玩家中秋节月饼制作数据
        $num = 8;
        /**
         * @var $cakeMakeList CakeMakeVoList
         * @var $cakeMakeData CakeMakeVo
         */
        $cakeMakeList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CAKE_MAKE);
        if ($cakeMakeList->getListCount() != $num){
            for ($i=1 ; $i <= $num ; $i++){
                if ($i <= 4){
                    $state = 1;
                }
                else{
                    $state = 0;
                }
                $cakeMakeData = $cakeMakeList->getElement($i,false);
                if (!$cakeMakeData){
                    //数据创建
                    $cakeMakeData = new CakeMakeVo(
                        $userId,//用户ID
                        $i,//制作栏位
                        0,//月饼类型
                        $state,//状态[0:未解锁，1：可制作，2：制作中，3：已完成]
                        0,//制作结束时间
                        $reqTime,//数据创建时间
                        $reqTime,//数据更新时间
                        true
                    );

                    if ($cakeMakeData->isInsert() && !SzUtility::checkArrayKey($i, $cakeMakeList->getList())) {
                        $cakeMakeList->addElement($cakeMakeData);
                        $cakeMakeList->setElement($i,$cakeMakeData);
                    }
                }
            }
        }
        else{
            //进行制作进度更新
            for ($i=1 ; $i <= $num ; $i++){
                $cakeMakeData = $cakeMakeList->getElement($i,false);
                $makeState = $cakeMakeData->getState();
                if ($makeState == 0 || $makeState == 1 || $makeState == 3){
                    continue;
                }

                $makeEndTime = $cakeMakeData->getMakeTime();
                if (!empty($makeEndTime) && $makeEndTime != 0 && $reqTime >= $makeEndTime){
                    $cakeMakeData->setState(3);
                    $cakeMakeData->setMakeTime($reqTime);
                    $cakeMakeData->setUpdateTime($reqTime);

                    $cakeMakeList->updateElement($i,$cakeMakeData);
                }
            }
        }
        SzPersister::get()->setVoList($cakeMakeList);

        $res['commonData'] = $midAutumnData->toArray();
        $res['makeCake'] = $cakeMakeList->toArray();
        return $res;
    }

    /**
     * 制作月饼
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $type
     * @param $flowerId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function cakeMake($userId, $activityId,$position, $type, $flowerId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'cake' => array(),
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节数据
        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);

        //获取玩家月饼制作数据
        /**
         * @var $cakeMakeList CakeMakeVoList
         * @var $cakeMakeData CakeMakeVo
         */
        $cakeMakeList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CAKE_MAKE);
        $cakeMakeData = $cakeMakeList->getElement($position,false);
        if (!$midAutumnData || !$cakeMakeData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $posState = $cakeMakeData->getState();
        if ($posState != 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['POS_ERR'];//栏位状态错误
            return $res;
        }

        //获取配置
        $cakeConfig = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_SYNTHESIS, $type, true);
        if (!$cakeConfig || empty($cakeConfig)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //制作月饼&道具消耗
        foreach ($cakeConfig['materialId'] as $itemId=>$count){
            GameService::get()->handleEntity($userId, $itemId, $count, true, true);
        }

        foreach ($cakeConfig['flowerId'] as $fid=>$num){
            $temp = '7401'.$flowerId;
            if ($fid == $temp){
                GameService::get()->handleEntity($userId, $fid, $num, true);
                break;
            }
            else{
                $temp = '';
            }
        }

        //制作数据更新
        $cakeMakeData->setType($type);
        $cakeMakeData->setState(2);
        $cakeMakeData->setMakeTime($reqTime + $cakeConfig['makeTime']);
        $cakeMakeData->setUpdateTime($reqTime);
        $cakeMakeList->updateElement($position,$cakeMakeData);
        SzPersister::get()->setVoList($cakeMakeList);

        $res['cake'] = $cakeMakeData->toArray();

        //获取玩家等级
        $userLevel = UserService::get()->getUserLevel($userId);
        //数据上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:midAutumn|action:cakeMake|',
                'item' => '|userlevel:'.($userLevel).'|cakeType:'.$type.'|makeTime:'.$reqTime.'|',
                'value' => 1
            )
        ));
        return $res;

    }

    /**
     * 解锁制作栏位
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function posUnlock($userId, $activityId, $position, $reqTime)
    {
        $res = array(
            'code' => 0,
            'data' => array(),
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节数据
        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);
        if (!$midAutumnData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $hasPosNum = $midAutumnData->getHasPos();
        $needUnlockPos = $position;
        //获取玩家中秋节月饼制作栏位数据
        /**
         * @var $cakeMakeList CakeMakeVoList
         * @var $cakeMakeData CakeMakeVo
         */
        $cakeMakeList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CAKE_MAKE);
        $cakeMakeData = $cakeMakeList->getElement($needUnlockPos,false);
        if (!$cakeMakeData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        if ($cakeMakeData->getState() > 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['POS_UNLOCKED'];//数据不存在
            return $res;
        }

        //进行道具消耗
        $needNumArr = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankHurdlesBuy', true)['value']);
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $needNumArr[$needUnlockPos-5], true, true);

        $cakeMakeData->setState(1);
        $cakeMakeData->setUpdateTime($reqTime);
        $cakeMakeList->updateElement($needUnlockPos,$cakeMakeData);
        SzPersister::get()->setVoList($cakeMakeList);

        $midAutumnData->setHasPos($hasPosNum + 1);
        $midAutumnData->setUpdateTime($reqTime);
        $midAutumnList->updateElement($activityId,$midAutumnData);
        SzPersister::get()->setVoList($midAutumnList);

        //数据上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:midAutumn|action:useCash|',
                'item' => '|userId:'.($userId).'|num:'.$needNumArr[$needUnlockPos-5].'|',
                'value' => 1
            )
        ));
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => '741010000001:'.$needNumArr[$needUnlockPos-5],
                'item' => 'midAutumn&useCash',
                'value' => 1
            )
        ));

        $res['data'] = $cakeMakeData->toArray();
        return $res;
    }

    /**
     * 清空cd时间or加速
     * @param $userId
     * @param $activityId
     * @param $speedType
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function clearCdTime($userId, $activityId, $speedType, $pos, $reqTime)
    {
        $res = array(
            'code' => 0,
            'order' => array(),
            'cake' => array()
        );

        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家等级
        $userLevel = UserService::get()->getUserLevel($userId);
        $orderData = array();
        $cakeData = array();
        $userNum = 0;
        $timeRange = 0;
        $needNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankAccelerate', true)['value'];
        if ($speedType == 1){//订单加速
            //获取玩家中秋节订单数据
            /**
             * @var $midAutumnOrdersList MidAutumnOrdersVoList
             * @var $midAutumnOrder MidAutumnOrdersVo
             */
            $midAutumnOrdersList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN_ORDERS);
            $midAutumnOrder = $midAutumnOrdersList->getElement($pos,false);
            if (!$midAutumnOrder){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
                return $res;
            }

            //计算加速需要钻石数量
            $cdEndTime = $midAutumnOrder->getCdTime();
            $timeRange = $cdEndTime - $reqTime;
            if (empty($cdEndTime) || $cdEndTime == 0 || $timeRange <= 0){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['NO_NEED_CLEAN_CD'];//不需要清理cd
                return $res;
            }
            else{
                $userNum = ceil($timeRange/(10*60))*$needNum;
                GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $userNum, true, true);

                //获取相关配置
                $tempWeigh = array();
                $quality = 0;
                $type = 1;
                $orderSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_ORDER, null, true);
                for($i=1;$i<=count($orderSetting);$i++){
                    $tempWeigh[] = array('type'=>$orderSetting[$i]['itemType'],'weight'=>$orderSetting[$i]['weight']);
                }

                $goldWeight = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankGoldenOrder', true)['value'];
                $tempWeigh2 = array(
                    array('quality' => 0, 'weight' => 100-$goldWeight),
                    array('quality' => 1, 'weight' => $goldWeight)
                );
                $quality = (CommonFunctionService::get()->weightRandom($tempWeigh2))['quality'];
                $type = (CommonFunctionService::get()->weightRandom($tempWeigh))['type'];

                $midAutumnOrder->setQuality($quality);
                $midAutumnOrder->setType($type);

                //数据更新
                $midAutumnOrder->setCdTime(0);
                $midAutumnOrder->setUpdateTime($reqTime);
                $midAutumnOrdersList->updateElement($pos,$midAutumnOrder);

                SzPersister::get()->setVoList($midAutumnOrdersList);
                $orderData = $midAutumnOrder->toArray();

                //数据上报
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                    'clientData' => array(
                        'tid' => 'Module:midAutumn|action:orderSpeed|',
                        'item' => '|userlevel:'.($userLevel).'|useNum:'.$userNum.'|orderSpeedTime:'.$reqTime.'|',
                        'value' => 1
                    )
                ));
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                    'clientData' => array(
                        'tid' => 'Module:midAutumn|action:useCash|',
                        'item' => '|userId:'.($userId).'|num:'.$userNum.'|',
                        'value' => 1
                    )
                ));
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
                    'clientData' => array(
                        'tid' => '741010000001:'.$userNum,
                        'item' => 'midAutumn&useCash',
                        'value' => 1
                    )
                ));
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
                    'clientData' => array(
                        'tid' => '741010000001:'.$userNum,
                        'item' => 'midAutumn&orderSpeed',
                        'value' => 1
                    )
                ));
            }
        }

        if ($speedType == 2){//制作加速
            //获取玩家中秋节月饼制作数据
            /**
             * @var $cakeMakeList CakeMakeVoList
             * @var $cakeMakeData CakeMakeVo
             */
            $cakeMakeList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CAKE_MAKE);
            $cakeMakeData = $cakeMakeList->getElement($pos,false);
            if (!$cakeMakeData){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
                return $res;
            }

            //计算加速需要钻石数量
            $makeEndTime = $cakeMakeData->getMakeTime();
            $timeRange = $makeEndTime - $reqTime;
            if ($cakeMakeData->getState() != 2 || $timeRange <= 0){//[0:未解锁，1：可制作，2：制作中，3：已完成]
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['NO_NEED_CLEAN_CD'];//不需要清理cd
                return $res;
            }

            $userNum = ceil($timeRange/(10*60))*$needNum;

            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $userNum, true, true);

            //数据更新
            $cakeMakeData->setState(3);
            $cakeMakeData->setMakeTime($reqTime);
            $cakeMakeData->setUpdateTime($reqTime);
            $cakeMakeList->updateElement($pos,$cakeMakeData);

            SzPersister::get()->setVoList($cakeMakeList);
            $cakeData = $cakeMakeData->toArray();

            //数据上报
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:midAutumn|action:makeSpeed|',
                    'item' => '|userlevel:'.($userLevel).'|useNum:'.$userNum.'|makeSpeedTime:'.$reqTime.'|',
                    'value' => 1
                )
            ));
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:midAutumn|action:useCash|',
                    'item' => '|userId:'.($userId).'|num:'.$userNum.'|',
                    'value' => 1
                )
            ));
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
                'clientData' => array(
                    'tid' => '741010000001:'.$userNum,
                    'item' => 'midAutumn&useCash',
                    'value' => 1
                )
            ));
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
                'clientData' => array(
                    'tid' => '741010000001:'.$userNum,
                    'item' => 'midAutumn&makeSpeed',
                    'value' => 1
                )
            ));
        }

        $res['order'] = $orderData;
        $res['cake'] = $cakeData;

        return $res;
    }

    /**
     * 完成月饼制作领奖
     * @param $userId
     * @param $activityId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getCakeMakeReward($userId, $activityId, $position, $reqTime)
    {
        $res = array(
            'code' => 0,
            'cake' => array(),
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节月饼制作栏位数据
        /**
         * @var $cakeMakeList CakeMakeVoList
         * @var $cakeMakeData CakeMakeVo
         */
        $cakeMakeList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CAKE_MAKE);
        $cakeMakeData = $cakeMakeList->getElement($position,false);
        if (!$cakeMakeData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        if ($cakeMakeData->getState() != 3){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['POS_ERR'];//状态错误
            return $res;
        }

        //获取配置
        $cakeConfig = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_SYNTHESIS, $cakeMakeData->getType(), true);
        //进行领取道具
        foreach ($cakeConfig['itemId'] as $item=>$count){
            GameService::get()->handleEntity($userId, $item, $count, true, true);
        }

        //数据更新
        $cakeMakeData->setState(1);
        $cakeMakeData->setType(0);
        $cakeMakeData->setMakeTime($reqTime);
        $cakeMakeData->setUpdateTime($reqTime);
        $cakeMakeList->updateElement($position,$cakeMakeData);
        SzPersister::get()->setVoList($cakeMakeList);

        $res['cake'] = $cakeMakeData->toArray();
        return $res;
    }

    /**
     * 获取月饼订单数据
     * @param $userId
     * @param $activityId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getOrdersInfo($userId, $activityId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'order' => array(),
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节订单数据
        $num = 6;
        /**
         * @var $midAutumnOrdersList MidAutumnOrdersVoList
         * @var $midAutumnOrder MidAutumnOrdersVo
         */
        $midAutumnOrdersList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN_ORDERS);

        //获取相关配置
        $tempWeigh = array();
        $quality = 0;
        $type = 1;
        $orderSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_ORDER, null, true);
        for($i=1;$i<=count($orderSetting);$i++){
            $tempWeigh[] = array('type'=>$orderSetting[$i]['itemType'],'weight'=>$orderSetting[$i]['weight']);
        }

        $goldWeight = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankGoldenOrder', true)['value'];
        $tempWeigh2 = array(
            array('quality' => 0, 'weight' => 100-$goldWeight),
            array('quality' => 1, 'weight' => $goldWeight)
        );

        if ($midAutumnOrdersList->getListCount() != $num){
            for ($i=1 ; $i <= $num ; $i++){
                $quality = (CommonFunctionService::get()->weightRandom($tempWeigh2))['quality'];
                $type = (CommonFunctionService::get()->weightRandom($tempWeigh))['type'];
                $midAutumnOrder = $midAutumnOrdersList->getElement($i,false);
                if (!$midAutumnOrder){
                    //数据创建
                    $midAutumnOrder = new MidAutumnOrdersVo(
                        $userId,//用户ID
                        $i,//订单号
                        $quality,//订单品质
                        $type,//月饼类型
                        0,//冷却时间
                        $reqTime,//数据创建时间
                        $reqTime,//数据更新时间
                        true
                    );

                    if ($midAutumnOrder->isInsert() && !SzUtility::checkArrayKey($i, $midAutumnOrdersList->getList())) {
                        $midAutumnOrdersList->addElement($midAutumnOrder);
                        $midAutumnOrdersList->setElement($i,$midAutumnOrder);
                    }
                }
            }
        }
        else{
            //进行订单更新
            for ($i=1 ; $i <= $num ; $i++){
                $midAutumnOrder = $midAutumnOrdersList->getElement($i,false);
                $cdTime = $midAutumnOrder->getCdTime();
                if (!empty($cdTime) && $cdTime != 0 && $reqTime >= $cdTime - 2){
                    $quality = (CommonFunctionService::get()->weightRandom($tempWeigh2))['quality'];
                    $type = (CommonFunctionService::get()->weightRandom($tempWeigh))['type'];

                    $midAutumnOrder->setQuality($quality);
                    $midAutumnOrder->setType($type);
                    $midAutumnOrder->setCdTime(0);
                    $midAutumnOrder->setUpdateTime($reqTime);

                    $midAutumnOrdersList->updateElement($i,$midAutumnOrder);
                }
            }
        }
        SzPersister::get()->setVoList($midAutumnOrdersList);

        $res['order'] = $midAutumnOrdersList->toArray();
        return $res;
    }

    /**
     * 提交月饼订单and领奖
     * @param $userId
     * @param $activityId
     * @param $orderId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function ordersSubmit($userId, $activityId, $orderId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'cdTime' => 0
        );

        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        //获取玩家中秋节数据
        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);

        /**
         * @var $midAutumnOrdersList MidAutumnOrdersVoList
         * @var $midAutumnOrder MidAutumnOrdersVo
         */
        $midAutumnOrdersList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN_ORDERS);
        $midAutumnOrder = $midAutumnOrdersList->getElement($orderId,false);

        if (!$midAutumnOrder || !$midAutumnData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $cdTime = $midAutumnOrder->getCdTime();
        if (!empty($cdTime) && $cdTime > $reqTime){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['IN_CD_TIME'];//在cd时间内
            return $res;
        }

        $type = $midAutumnOrder->getType();
        $quality = $midAutumnOrder->getQuality();
        //获取配置表数据
        $synthesisConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_SYNTHESIS, $type, true);
        $orderConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_ORDER, $type, true);
        if (!$synthesisConf || !$orderConf || empty($synthesisConf) || empty($orderConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        $needNum = 0;
        if ($quality == 0){
            $needNum = $orderConf['ordinaryNum'];
        }
        if ($quality == 1){
            $needNum = $orderConf['GoldenNum'];
        }
        /**
         * @var ModuleItemVoList $itemList
         */
        //玩家材料是否足够
        list($itemDefId, $num) = (reset($synthesisConf['itemId']) ? each($synthesisConf['itemId']) : each($synthesisConf['itemId']));
        $itemDefId = substr($itemDefId,4);
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $ownCount = $itemList->getItemDefCount($itemDefId);
        if ($ownCount < $needNum){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
            return $res;
        }

        //道具消耗
        GameService::get()->handleEntity($userId, '7401'.$itemDefId, $needNum, true, true);

        //金币获得
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_TOKEN, $synthesisConf['itemGold']*$needNum, true);
        //经验获得
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_EXP, $synthesisConf['itemExp']*$needNum, true);

        //订单更新
        $cdRange = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankOrderCD', true)['value'];
        $midAutumnOrder->setCdTime($reqTime + $cdRange*60);
        $midAutumnOrder->setUpdateTime($reqTime);
        $midAutumnOrdersList->updateElement($orderId,$midAutumnOrder);
        SzPersister::get()->setVoList($midAutumnOrdersList);

        //获取排行榜奖励领奖时间
        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
        if ($rankRewardTime >= $reqTime){
            //积分添加
            $nowPoint = $midAutumnData->getPoint();
            $midAutumnData->setPoint($nowPoint + $needNum);
            $midAutumnList->updateElement($activityId,$midAutumnData);
            SzPersister::get()->setVoList($midAutumnList);

            //排行榜更新数据
            $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
            $redisDb->zAdd(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,$nowPoint + $needNum,$userId);
        }

        $res['cdTime'] = $reqTime + $cdRange*60;

        //获取玩家等级
        $userLevel = UserService::get()->getUserLevel($userId);
        //数据上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:midAutumn|action:ordersSubmit|',
                'item' => '|userlevel:'.($userLevel).'|orderTime:'.$reqTime.'|',
                'value' => 1
            )
        ));
        return $res;
    }

    /**
     * 中秋商店购买
     * @param $userId
     * @param $activityId
     * @param $type
     * @param $goodId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shopping($userId, $activityId, $type, $goodId,$reqTime)
    {
        $res = array(
            'code' => 0,
            'free' => 0,
            'diamond' => false
        );

        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        $canFreeTime = 0;
        $diamondState = false;

        //获取玩家中秋节数据
        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);
        if (!$midAutumnData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $freeAdmissionTime = $midAutumnData->getFreeAdmissionTime();
        if ($type == 1){//免费领取模块
            if ($freeAdmissionTime <= $reqTime){
                //进行道具发放
                $freeReward = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankFreeMaterial', true)['value']);
                foreach ($freeReward as $itemId=>$count){
                    GameService::get()->handleEntity($userId, $itemId, $count, true, true);
                }
            }
            else{
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['IN_CD_TIME'];//在cd时间内
                $res['free'] = $freeAdmissionTime;
                return $res;
            }

            $cdRange = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankFreeCD', true)['value'];

            //数据更新
            $canFreeTime = $reqTime + $cdRange*60;
            $midAutumnData->setFreeAdmissionTime($canFreeTime);
            $midAutumnData->setUpdateTime($reqTime);
            $midAutumnList->updateElement($activityId,$midAutumnData);
        }

        if ($type == 2){//钻石购买模块
            if ($goodId == 0 || empty($goodId) || $goodId == null){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PARAMS_ERR'];//参数错误
                return $res;
            }

            $buyConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_STORE, $goodId, true);
            if (empty($buyConf) || !$buyConf || $buyConf == null){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//数据不存在
                return $res;
            }

            //进行钻石消耗
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $buyConf['itemPrice'], true, true);
            //进行道具发放
            foreach ($buyConf['itemId'] as $itemId=>$count){
                GameService::get()->handleEntity($userId, $itemId, $count, true, true);
            }

            $diamondState = true;
            $canFreeTime = $midAutumnData->getFreeAdmissionTime();

            //获取玩家等级
            $userLevel = UserService::get()->getUserLevel($userId);
            //数据上报
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:midAutumn|action:shop|',
                    'item' => '|userlevel:'.($userLevel).'|buyTime:'.$reqTime.'|',
                    'value' => 1
                )
            ));
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:midAutumn|action:useCash|',
                    'item' => '|userId:'.($userId).'|num:'.$buyConf['itemPrice'].'|',
                    'value' => 1
                )
            ));
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
                'clientData' => array(
                    'tid' => '741010000001:'.$buyConf['itemPrice'],
                    'item' => 'midAutumn&useCash',
                    'value' => 1
                )
            ));
        }

        SzPersister::get()->setVoList($midAutumnList);

        $res['free'] = $canFreeTime;
        $res['diamond'] = $diamondState;
        return $res;
    }

    /**
     * 中秋节积分排行榜
     * @param $userId
     * @param $activityId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function midAutumnRankInfo($userId, $activityId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'myRank' => array(),
            'randData' => array()
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }

        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);

        $score = $rank = 0;
        $myRank = 999;//默认初始
        if ($midAutumnData) {
            $score = $midAutumnData->getPoint();
        }

        $res['myRank'] = array(
            'index' => $myRank,
            'score' => $score
        );

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $updateDataTime = $redisDb->get(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_UPDATE.$userId);
        if (!$updateDataTime){
            $redisDb->set(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_UPDATE.$userId,$reqTime+300);
            $redisDb->zAdd(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,$score,$userId);
        }
        else{
            if ($updateDataTime < $reqTime){
                $redisDb->set(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_UPDATE.$userId,$reqTime+300);
                $redisDb->zAdd(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,$score,$userId);
            }
        }
        $rankData = $redisDb->zRevRange(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,0,199,true);

        if ($rankData) {
            foreach ($rankData as $uid=>$number) {
                $rank++;
                if ($rank <= 100){
                    $res['randData'][] = CommonFunctionService::get()->getUserInfo($uid,$number,1);//ActivityService::get()->getUserInfo($uid,$number)
                }

                if ($uid == $userId) {
                    $res['myRank']['index'] = $rank;
                    $res['myRank']['score'] = $score;
                }
            }
        }

        return $res;
    }

    /**
     * 中秋节排行榜最终领奖
     * @param $userId
     * @param $activityId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function midAutumnRankGetReward($userId, $activityId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'reward' => array()
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
        //获取领奖时间
        $rewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
        if ($rewardTime > $reqTime){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REWARD_TIME_NOT_ARRIVED'];//领奖时间未到
            return $res;
        }

        $midAutumnSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_MID_AUTUMN_RANK, null, true);
        if (!$midAutumnSetting || empty($midAutumnSetting)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);
        if (!$midAutumnData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        if ($midAutumnData->getFinalStatus()) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REWARD_HAS_GET'];//活动奖励已经领取
            return $res;
        }

        $rank = 0;
        $score = $midAutumnData->getPoint();//玩家最新积分

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->zAdd(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,$score,$userId);
        $rankData = $redisDb->zRevRange(GameConst::CACHE_KEY_ACTIVITY_MID_AUTUMN_RANK.$activityId,0,199,true);

        if ($rankData) {
            foreach ($rankData as $uid=>$number) {
                $rank++;
                if ($uid == $userId) {
                    break;
                }
            }
        }

        //定位玩家属于那一档为奖励
        $reward = array();
        $rewardLevel = 0;
        $min = 0;
        $max = 0;
        foreach ($midAutumnSetting as $level=>$rewardConf){
            $min = $rewardConf['rankingRange'][0];
            $max = $rewardConf['rankingRange'][1];
            if ($rank > $min && $rank <= $max){
                $reward = $rewardConf['rankingReward'];
                $res['reward'] = $rewardConf['rankingReward'];
                break;
            }
        }

        if (empty($reward) || !$reward){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        $output = '';
        //发送奖励
        foreach ($reward as $entryId=>$count) {
            GameService::get()->handleEntity($userId, $entryId, $count, true, true);
            $output .= $entryId.':'.$count.'|';
        }

        $midAutumnData->setFinalStatus(1);
        $midAutumnData->setUpdateTime($reqTime);
        $midAutumnList->updateElement($activityId,$midAutumnData);
        SzPersister::get()->setVoList($midAutumnList);

        return $res;
    }

    /**
     * 中秋节积分领奖
     * @param $userId
     * @param $activityId
     * @param $step
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function midAutumnPointGetReward($userId, $activityId, $step ,$reqTime)
    {
        $res = array(
            'code' => 0,
            'reward' => ''
        );
        $checkData = CommonFunctionService::get()->checkActivityState($userId, $activityId, self::ActivityMidAutumn,$reqTime);
        //校验活动是否开启
        if ($checkData['code'] != 0){
            $res['code'] = $checkData['code'];
            return $res;
        }
//        //获取排行榜奖励领奖时间
//        $rankRewardTime = strtotime(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankRewardGet', true)['value']);
//        if ($rankRewardTime < $reqTime){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RANK_GET_REWARD_TIME'];//排行榜领奖阶段
//            return $res;
//        }

        /**
         * @var $midAutumnList MidAutumnVoList
         * @var $midAutumnData MidAutumnVo
         */
        $midAutumnList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_MID_AUTUMN);
        $midAutumnData = $midAutumnList->getElement($activityId,false);
        if (!$midAutumnData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $score = $midAutumnData->getPoint();//玩家最新积分

        //获取积分段奖励配置
        $pointArr = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankPointNum', true)['value']);
        $rewardArr = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'rankPointReward', true)['value']);

        if (!$pointArr || empty($pointArr) || !$rewardArr || empty($rewardArr)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        $needPoint = $pointArr[$step - 1];
        $getItemId = 0;
        $num = 0;
        $i = 1;
        foreach ($rewardArr as $itemId=>$count){
            if ($i == $step){
                $getItemId = $itemId;
                $num = $count;
                break;
            }
            $i++;
        }

        if ($getItemId == 0 || $num == 0 || $needPoint == 0 || empty($needPoint)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        if ($score < $needPoint){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['POINT_NOT_ENOUGH'];//积分不足
            return $res;
        }

        $rewardSituation = explode(';',json_decode($midAutumnData->getReward()));
        $rewardState = $rewardSituation[$step - 1];
        if ($rewardState == 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REWARD_HAS_GET'];//活动奖励已经领取
            return $res;
        }

        //发送奖励
        GameService::get()->handleEntity($userId, $getItemId, $num, true, true);
        $rewardSituation[$step - 1] = 1;
        $midAutumnData->setUpdateTime($reqTime);
        $midAutumnData->setReward(implode(';',$rewardSituation));
        $midAutumnList->updateElement($activityId,$midAutumnData);
        SzPersister::get()->setVoList($midAutumnList);

        $res['reward'] = json_decode($midAutumnData->getReward());
        return $res;
    }

    /**
     * 获得头像框
     * @param $userId
     * @param $type
     * @param $itemId
     * @return mixed
     * @throws SzException
     */
    public function getHeadFrame($userId, $type, $itemId)
    {
        $nowTime = GameService::get()->getReqTime();
        /**
         * @var $userDressList UserDressVoList
         * @var $userDressData UserDressVo
         */
        $userDressList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_USER_DRESS);
        $userDressData = $userDressList->getElement($type,false);
        if (!$userDressData){
            $userDressData = new UserDressVo(
                $userId,
                $type,
                json_encode((string)$itemId),
                $nowTime,
                $nowTime,
                true
            );
            if ($userDressData->isInsert() && !SzUtility::checkArrayKey($type, $userDressList->getList())) {
                $userDressList->addElement($userDressData);
                $userDressList->setElement($type,$userDressData);
            }
        }
        else{
            $headFrameLibrary = json_decode($userDressData->getStock());
            if ($headFrameLibrary != ''){
                $headFrameArr = explode(',',$headFrameLibrary);
                if (!in_array($itemId,$headFrameArr)){
                    $headFrameArr[] = $itemId;
                    $userDressData->setStock(implode(',',$headFrameArr));
                }
            }
            else{
                $userDressData->setStock(json_encode($itemId));
            }
            $userDressList->updateElement($type,$userDressData);
        }

        SzPersister::get()->setVoList($userDressList);
    }

    /**
     * 获取玩家所有头像框
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function userHeadFrames($userId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'headFrames' => array()
        );

        /**
         * @var $userDressList UserDressVoList
         * @var $userDressData UserDressVo
         */
        $userDressList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_USER_DRESS);
        $userDressData = $userDressList->getElement(GameConst::ITEM_SPECIAL_SMALL_TYPE['Head_frame'],false);
        if (!$userDressData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['HEAD_FRAME_NOT_EXIST'];//头像框不存在
            return $res;
        }
        else{
            $headFrameLibrary = $userDressData->getStock();
            if (json_decode($headFrameLibrary) != ''){
                $res['headFrames'] = explode(',',json_decode($headFrameLibrary));
            }
        }

        return $res;
    }

    /**
     * 设置当前的头像框
     * @param $userId
     * @param $itemId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function setUseHeadFrame($userId, $itemId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'headFrame' => 0
        );

        /**
         * @var ModuleProfileVo $profileInfo
         * @var ModuleProfileInfoVoList $profileInfoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_HEAD_FRAME, false);

        if ($itemId != 0){
            $type = substr($itemId , 0 , 2);
            /**
             * @var $userDressList UserDressVoList
             * @var $userDressData UserDressVo
             */
            $userDressList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_USER_DRESS);
            $userDressData = $userDressList->getElement($type,false);
            if (!$userDressData){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
                return $res;
            }
            else{
                $headFrameLibrary = $userDressData->getStock();
                $headFrameArr = explode(',',json_decode($headFrameLibrary));
                if (!in_array($itemId,$headFrameArr)){
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['HEAD_FRAME_NOT_EXIST'];//头像不存在
                    return $res;
                }
            }
        }

        //设置当前使用的头像框
        if (!$profileInfo) {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_HEAD_FRAME,
                $itemId,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_HEAD_FRAME, $profileInfo);
        }
        else{
            $profileInfo->setInfo($itemId);
            $profileInfo->setTime($reqTime);
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_HEAD_FRAME, $profileInfo);
        }

        SzPersister::get()->setVoList($profileInfoList);
        $res['headFrame'] = $itemId;


        return $res;
    }

    /**
     * @param $userId
     * @param $imgId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function setAvatarImg($userId, $imgId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'avatarImg' => 0
        );

        /**
         * @var ModuleProfileVo $profileInfo
         * @var ModuleProfileInfoVoList $profileInfoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_HEAD_IMG_ID, false);
        //设置当前使用的头像框
        if (!$profileInfo) {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_HEAD_IMG_ID,
                $imgId,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_HEAD_IMG_ID, $profileInfo);
        }
        else{
            $profileInfo->setInfo($imgId);
            $profileInfo->setTime($reqTime);
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_HEAD_IMG_ID, $profileInfo);
        }

        SzPersister::get()->setVoList($profileInfoList);
        $res['avatarImg'] = $imgId;


        return $res;
    }
}